package com.sduept.nwld.dataserver.service.fault;

import com.alibaba.fastjson.JSONArray;
import com.sduept.bigdata.fault.entity.FaultSourceUploadInfo;
import com.sduept.bigdata.fault.manager.DeviceFaultCountManager;
import com.sduept.nwld.dataserver.manager.fault.FaultSourceQueryManager;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author huangyu
 * @Date 2019年6月19日
 * 
 * 故障源统计柱状图展示
 */
@RestController
@RequestMapping("/faultCount/")
public class DeviceFaultCountService {
	
	@Autowired
	private FaultSourceQueryManager uplaodInfoQueryM;
	@Autowired
	private DeviceFaultCountManager countM;
	@Autowired
	private FaultSourceQueryManager fm;


	@GetMapping("/getLineFaultUploadCountInfos/{startTime}/{endTime}/{voltage}")
	public String getLineFaultUploadCountInfos(@PathVariable("startTime") String startTime,@PathVariable("endTime") String endTime,@PathVariable("voltage") String voltage) {
		startTime += " 00:00:00";
		Date startDate = DateUtil.getDateByString(startTime, DateUtil.pattern_ymdhms);
		endTime += " 23:59:59";
		Date endDate = DateUtil.getDateByString(endTime, DateUtil.pattern_ymdhms);
		Integer vol = null;
		if (!StringUtils.isEmpty(voltage)) {
			voltage = voltage.replace("kV", "");
			vol = new Integer(voltage);
		}
		List<FaultSourceUploadInfo> uploadInfos = fm.findLineFaultUploadInfoByDateAndVoltage(startDate, endDate, vol,"线路");
		return countM.genChartJSONData(uploadInfos);
	}
	

	@GetMapping("/getUploadInfos/{year}/{month}")
	public String getUploadInfosByCondition(@PathVariable("year") String year,@PathVariable("month") String month) {
		List<FaultSourceUploadInfo> infos = uplaodInfoQueryM.findFaultSourceUploadInfo(Integer.parseInt(year), Integer.parseInt(month));
		String jsonStr = countM.genChartJSONData(infos);
		return jsonStr;
	}


	@GetMapping("/getYearCountInfos/{year}")
	public String getFaultUploadYearCountInfos(@PathVariable("year") String year) {
		JSONArray jsonStr = countM.getJSONData(year);
		return jsonStr.toString();
	}

}
